package shared;

public class WordList extends URLList{
	
	//Instance variables
	public WordList next;
	
	/**
	 * WordList constructor
	 * @param s String 
	 * @param url URLList
	 * @param n WordList
	 */
	public WordList (String s, URLList url, WordList n) {
		super(s,url);
		next = n;
	}
	
    /**
     * toString is called recursively on the list
     */
    public String toString(){
    	//Set the return value to this.str
    	String returnString = str;
    	returnString += "-" + nexturl.toString() + "-";
    	//Is there exist a next element
    	if(next != null){
    		
    		//Add new line to return value
    		returnString += "\n";
    		
    		//Call toString on next element
    		returnString += next.toString();
    	}    	
    	
    	//Return string
    	return returnString;
    }
    
    /**
     * equals evaluate if two WordList objects are equal
     * 
     * @param object to compare
     */
    public boolean equals (Object o)
    {
    	//Create return value
    	boolean returnValue = false;
    	
    	//Check if o is an WordList object and not null
    	if(o instanceof WordList && o != null){
    		//Create objects to compare
    		WordList thisList = this;
    		WordList compareList = (WordList) o;
    		
    		//Check for each element in the list for both lists
    		while(thisList != null && compareList != null){
    			
    			//Assume they are equale
				returnValue = true;
				
				//If the string on the list elements are not equal
    			if(!thisList.str.equals(compareList.str)){
    				//Set return value to false and break
    				returnValue = false;
    				break;
    			
    			//It the urls are not equal
    			} else if (!thisList.nexturl.equals(compareList.nexturl)){
    				//Set return value to false and break
    				returnValue = false;
    				break;
    			}
    			
    			//If the lists have different length
    			if (thisList.next == null && compareList.next != null){
    				//Set return value to false and break
    				returnValue = false;
    				break;
    			}
    			if (thisList.next != null && compareList.next == null){
    				//Set return value to false and break
    				returnValue = false;
    				break;
    			}
    			
    			//Get next element in the lists
    			thisList = thisList.next;
    			compareList = compareList.next;
    		}
    	}    	
    	
    	//Return value
    	return returnValue;
    }
}
